import axios from 'axios';

import Cookies from 'js-cookie';
var api;

if (process.env.NODE_ENV === 'production') {
    api = axios.create({
        baseURL: '/'
    });
} else {
    api = axios.create({
        baseURL: 'http://localhost:63042/',
        withCredentials: true
    });
}

api.interceptors.request.use(function (config) {
    app.$Loading.start();
    return config;
}, function (error) {
    return Promise.reject(error);
});
api.interceptors.response.use(function (res) {
    app.$Loading.finish();
    if (res.data.code==200) {
        return res;
    } else {
        if(res.data.code==401){
            if(app.$route.path!='/login'){
                app.$Message.error(res.data.msg);                
                Cookies.set('user',  '',-1);
                Cookies.set('userinfo', '',-1);
                app.$router.push({name:"login"});
                return null;
            }
        }
        if(res.data.code==403){
            if(app.$route.path!='/login'&&app.$route.path!='/'&&app.$route.path!='/locking'){
                console.log(app.$route)
                app.$Message.error(res.data.msg);
                app.$router.push({
                    name: 'error-403'
                });
            }
            // app.router.push({
            //     name: 'error-403'
            // });
        }
        return res;
    }
}, function (error) {
    app.$Loading.error();
    app.$Message.error('请求失败');
    return Promise.reject(error);
});

export default api;